package com.szqhyxj.youxiangju.biz.equipmentActivation.activationrecord;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by leung on 2018/11/4.
 */

public class ActivationRecordAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<ActivationRecordModel.RecordEntity> mDatas;

    //MyAdapter需要一个Context，通过Context获得Layout.inflater，然后通过inflater加载item的布局
    public ActivationRecordAdapter(Context context, List<ActivationRecordModel.RecordEntity> datas) {

        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    //返回数据集的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //这个方法才是重点，我们要为它编写一个ViewHolder
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activation_record_item, parent, false); //加载布局
            holder = new ViewHolder();


            holder.slotNum = convertView.findViewById(R.id.slotNum);
            holder.price = convertView.findViewById(R.id.price);
            holder.time = convertView.findViewById(R.id.time_record);
            holder.paytype = convertView.findViewById(R.id.paytype);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }
        holder.slotNum.setText("激活" + mDatas.get(position).getPortCount() + "个端口");
        String money = mDatas.get(position).getPayMoney();
        holder.price.setText("￥" + money.substring(0,money.length()-2));
        if (mDatas.get(position).getPayDate()==null){
            holder.time.setText("");
        }else{
            holder.time.setText(mDatas.get(position).getPayDate() + "");
        }

        if (mDatas.get(position).getPayType() == 1) {
            holder.paytype.setText("支付宝");

        }
        if (mDatas.get(position).getPayType() == 2) {
            holder.paytype.setText("微信");

        }
        if (mDatas.get(position).getPayType() == 3) {
            holder.paytype.setText("信用卡");

        }
        if (mDatas.get(position).getPayType() == 4) {
            holder.paytype.setText("储蓄卡");

        }

        return convertView;
    }

    static

    //这个ViewHolder只能服务于当前这个特定的adapter，因为ViewHolder里会指定item的控件，不同的ListView，item可能不同，所以ViewHolder写成一个私有的类
    private class ViewHolder {
        TextView slotNum;
        TextView price;
        TextView time;
        TextView paytype;
    }


}